package com.easylinkin.linkappapi.reportCenter.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.easylinkin.linkappapi.reportCenter.entity.ReportAiDetail;
import com.easylinkin.linkappapi.reportCenter.entity.dto.ReportDetailDTO;
import com.easylinkin.linkappapi.reportCenter.entity.vo.ReportAiVo;
import com.easylinkin.linkappapi.reportCenter.mapper.ReportAiMapper;
import com.easylinkin.linkappapi.reportCenter.service.ReportAiService;
import com.easylinkin.linkappapi.reportCenter.timer.DeviceReportTimer;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @Description ai预警
 * @Author kan yuanfeng
 * @Date 2023/7/17 14:27
 */
@Service
public class ReportAiServiceImpl extends ReportBaseServiceImpl<ReportAiVo> implements ReportAiService {
    @Autowired
    private ReportAiMapper reportAiMapper;
    @Autowired
    private DeviceReportTimer deviceReportTimer;

    private String aiEvent = "安全帽,反光衣";

    @Override
    protected List<ReportAiVo> getByReportId(Integer reportId,Integer type) {
        List<ReportAiVo> reportAiVos = reportAiMapper.findByReportId(reportId);
        return reportAiVos;
    }

    @Override
    protected List<ReportAiVo> getLastByDate(String tenantId,Date date,Integer type) {
        //根据时间获取ai预警
        List<ReportAiVo> reportAiVos = deviceReportTimer.countAi(tenantId, date, null);
        return reportAiVos;
    }

    /**
     * 当月数据处理
     * @param reportId
     * @param name
     * @param type (两个数据在一张表时才有)
     * @return
     */
    @Override
    public ReportDetailDTO dataHandler(Integer reportId, String name, Integer type) {
        ReportDetailDTO reportDetailDTO = this.dataHandler(reportId, name, type, (cur, last) -> {
            List<ReportAiDetail> detailList = new ArrayList<>();
            List<ReportAiDetail> curDetailList = cur.getDetailList();
            List<ReportAiDetail> lastDetailList = last.getDetailList();
            Arrays.stream(aiEvent.split(",")).forEach(a->{
                ReportAiDetail reportAiDetail = new ReportAiDetail();
                reportAiDetail.setEvent(a.equals("安全帽")?"未佩戴安全帽":"未穿戴反光衣");
                if (CollectionUtil.isNotEmpty(curDetailList)){
                    int sum = curDetailList.stream().filter(la -> StringUtils.isNotBlank(la.getEvent()) && la.getEvent().contains(a)).mapToInt(la -> la.getNum()).sum();
                    reportAiDetail.setNum(sum);
                }else {
                    reportAiDetail.setNum(0);
                }
                if (CollectionUtil.isNotEmpty(lastDetailList)){
                    int sum = lastDetailList.stream().filter(la -> StringUtils.isNotBlank(la.getEvent()) && la.getEvent().contains(a)).mapToInt(la -> la.getNum()).sum();
                    reportAiDetail.setLastNum(sum);
                }else {
                    reportAiDetail.setLastNum(0);
                }
                reportAiDetail.computeRatio();
                detailList.add(reportAiDetail);
                cur.setDetailList(detailList);
            });
//            List<ReportAiDetail> lastDetailList = last.getDetailList();
//            if (null != lastDetailList){
//                List<ReportAiDetail> detailList = cur.getDetailList();
//                if (CollectionUtil.isNotEmpty(detailList)){
//                    detailList.forEach(d->{
//                        ReportAiDetail reportAiDetail = lastDetailList.stream().filter(la -> d.getEvent().equals(la.getEvent())).findFirst().orElse(null);
//                        if (null != reportAiDetail){
//                            d.setLastNum(reportAiDetail.getNum());
//                            d.computeRatio();
//                        }
//                    });
//                }
//            }
        });
        return reportDetailDTO;
    }
}
